#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
/*
* This is a freestanding application, so we want to use alt_main
* as the entry point. However, if the debugger is run on this
* application, it will try to set a breakpoint at main, which
* the application does not contain. The below line creates an
* alias so the debugger is able to set a breakpoint at main,
* yet the application retains alt_main as itâ€™s entry point.
*/
int main (void) __attribute__ ((weak, alias ("alt_main")));
/*
* Use alt_main as entry point for this free-standing application
*/
int alt_main (void)
{
    alt_u16 led = 0x2;
    alt_u8 dir = 0;
    volatile int i;
    /*
    * Infinitely shift a variable with one bit set back and forth, and write
    * it to the LED PIO. Software loop provides delay element.
    */
    while (1)
    {
        if (led & 0x201)
        {
            dir = (dir ^ 0x1);
        }
        
        if (dir)
        {
            led = led >> 1;
        }
        else
        {
            led = led << 1;
        }
        
        IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, ~led);
        i = 0;
        
        while (i<40000)
        {
            i++;
        }
    }
    
    return 0;
}
